# Copyright 2020 The XLS Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

package(
    default_applicable_licenses = ["//:license"],
    default_visibility = ["//xls:xls_internal"],
    features = [
        "layering_check",
        "parse_headers",
    ],
    licenses = ["notice"],  # Apache 2.0
)

cc_library(
    name = "stage_conversion",
    srcs = ["stage_conversion.cc"],
    hdrs = ["stage_conversion.h"],
    deps = [
        "//xls/codegen:codegen_options",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:channel",
        "//xls/ir:function_builder",
        "//xls/ir:name_uniquer",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "//xls/ir:type",
        "//xls/ir:value",
        "//xls/scheduling:pipeline_schedule",
        "//xls/scheduling:schedule_graph",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "stage_conversion_test",
    srcs = ["stage_conversion_test.cc"],
    deps = [
        ":stage_conversion",
        ":state_to_channel_conversion_pass",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/interpreter:channel_queue",
        "//xls/interpreter:evaluator_options",
        "//xls/interpreter:interpreter_proc_runtime",
        "//xls/interpreter:proc_runtime",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:function_builder",
        "//xls/ir:ir_test_base",
        "//xls/ir:proc_elaboration",
        "//xls/ir:value",
        "//xls/ir:verifier",
        "//xls/passes:pass_base",
        "//xls/scheduling:pipeline_schedule",
        "//xls/scheduling:run_pipeline_schedule",
        "//xls/scheduling:scheduling_options",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "stage_conversion_pass",
    srcs = ["stage_conversion_pass.cc"],
    hdrs = ["stage_conversion_pass.h"],
    deps = [
        ":stage_conversion",
        "//xls/codegen:codegen_pass",
        "//xls/common/logging:log_lines",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/passes:pass_base",
        "//xls/scheduling:pipeline_schedule",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status:statusor",
    ],
)

cc_library(
    name = "stage_conversion_pass_pipeline",
    srcs = ["stage_conversion_pass_pipeline.cc"],
    hdrs = ["stage_conversion_pass_pipeline.h"],
    deps = [
        ":stage_conversion_pass",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/passes:optimization_pass",
    ],
)

cc_test(
    name = "stage_conversion_pass_pipeline_test",
    srcs = ["stage_conversion_pass_pipeline_test.cc"],
    deps = [
        ":stage_conversion",
        ":stage_conversion_pass_pipeline",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/interpreter:channel_queue",
        "//xls/interpreter:evaluator_options",
        "//xls/interpreter:interpreter_proc_runtime",
        "//xls/interpreter:proc_runtime",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:function_builder",
        "//xls/ir:ir_test_base",
        "//xls/ir:proc_elaboration",
        "//xls/ir:value",
        "//xls/ir:verifier",
        "//xls/passes:optimization_pass",
        "//xls/passes:pass_base",
        "//xls/scheduling:pipeline_schedule",
        "//xls/scheduling:run_pipeline_schedule",
        "//xls/scheduling:scheduling_options",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "state_to_channel_conversion_pass",
    srcs = ["state_to_channel_conversion_pass.cc"],
    hdrs = ["state_to_channel_conversion_pass.h"],
    deps = [
        ":stage_conversion",
        "//xls/codegen:codegen_pass",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:channel",
        "//xls/ir:channel_ops",
        "//xls/ir:state_element",
        "//xls/ir:type",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "state_to_channel_conversion_pass_test",
    srcs = ["state_to_channel_conversion_pass_test.cc"],
    deps = [
        ":state_to_channel_conversion_pass",
        "//xls/codegen:codegen_pass",
        "//xls/common:xls_gunit_main",
        "//xls/common/status:matchers",
        "//xls/common/status:status_macros",
        "//xls/interpreter:channel_queue",
        "//xls/interpreter:proc_evaluator",
        "//xls/interpreter:proc_interpreter",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:channel_ops",
        "//xls/ir:function_builder",
        "//xls/ir:ir_test_base",
        "//xls/ir:proc_elaboration",
        "//xls/ir:value",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "block_utils",
    hdrs = ["block_utils.h"],
    deps = [
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library(
    name = "passes_ng_test_fixtures",
    testonly = True,
    hdrs = ["passes_ng_test_fixtures.h"],
    deps = [
        ":block_channel_slot",
        ":stage_conversion",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:block_conversion_test_fixture",
        "//xls/codegen:codegen_options",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/interpreter:block_evaluator",
        "//xls/interpreter:ir_interpreter",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:function_builder",
        "//xls/ir:ir_test_base",
        "//xls/ir:register",
        "//xls/ir:source_location",
        "//xls/ir:verifier",
        "//xls/scheduling:pipeline_schedule",
        "//xls/scheduling:run_pipeline_schedule",
        "//xls/scheduling:scheduling_options",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "block_channel_slot",
    srcs = ["block_channel_slot.cc"],
    hdrs = ["block_channel_slot.h"],
    deps = [
        ":block_utils",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "block_channel_slot_test",
    srcs = ["block_channel_slot_test.cc"],
    deps = [
        ":block_channel_slot",
        ":passes_ng_test_fixtures",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir",
        "//xls/ir:op",
        "//xls/ir:verifier",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "block_channel_adapter",
    srcs = ["block_channel_adapter.cc"],
    hdrs = ["block_channel_adapter.h"],
    deps = [
        ":block_channel_slot",
        ":block_utils",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "//xls/ir:value",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "block_channel_adapter_test",
    srcs = ["block_channel_adapter_test.cc"],
    deps = [
        ":block_channel_adapter",
        ":passes_ng_test_fixtures",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:function_builder",
        "//xls/ir:ir_matcher",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "//xls/ir:value",
        "//xls/ir:verifier",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "stage_to_block_conversion_metadata",
    hdrs = ["stage_to_block_conversion_metadata.h"],
    deps = [
        ":block_channel_adapter",
        ":block_channel_slot",
        ":stage_conversion",
        "//xls/ir",
        "//xls/ir:channel",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "stage_to_block_conversion",
    srcs = ["stage_to_block_conversion.cc"],
    hdrs = ["stage_to_block_conversion.h"],
    deps = [
        ":block_channel_adapter",
        ":block_channel_slot",
        ":stage_conversion",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_options",
        "//xls/codegen:conversion_utils",
        "//xls/common:casts",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "//xls/ir:value",
        "//xls/ir:value_utils",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "stage_to_block_conversion_pass",
    srcs = ["stage_to_block_conversion_pass.cc"],
    hdrs = ["stage_to_block_conversion_pass.h"],
    deps = [
        ":stage_conversion",
        ":stage_to_block_conversion",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/common/logging:log_lines",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status:statusor",
    ],
)

cc_test(
    name = "stage_to_block_conversion_pass_test",
    srcs = ["stage_to_block_conversion_pass_test.cc"],
    deps = [
        ":passes_ng_test_fixtures",
        ":stage_conversion",
        ":stage_to_block_conversion_pass",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir:function_builder",
        "//xls/ir:verifier",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "block_pipeline_inserter",
    srcs = ["block_pipeline_inserter.cc"],
    hdrs = ["block_pipeline_inserter.h"],
    deps = [
        ":block_channel_slot",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_options",
        "//xls/codegen:conversion_utils",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:op",
        "//xls/ir:register",
        "//xls/ir:source_location",
        "//xls/ir:value",
        "//xls/ir:value_utils",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "block_pipeline_inserter_test",
    srcs = ["block_pipeline_inserter_test.cc"],
    deps = [
        ":block_channel_slot",
        ":block_pipeline_inserter",
        ":passes_ng_test_fixtures",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_options",
        "//xls/common:xls_gunit_main",
        "//xls/common/status:matchers",
        "//xls/ir",
        "//xls/ir:channel",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "block_pipeline_inserter_pass",
    srcs = ["block_pipeline_inserter_pass.cc"],
    hdrs = ["block_pipeline_inserter_pass.h"],
    deps = [
        ":block_pipeline_inserter",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/common/logging:log_lines",
        "//xls/common/status:ret_check",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status:statusor",
    ],
)

cc_test(
    name = "stage_to_block_conversion_test",
    srcs = ["stage_to_block_conversion_test.cc"],
    deps = [
        ":block_channel_adapter",
        ":block_channel_slot",
        ":block_pipeline_inserter",
        ":passes_ng_test_fixtures",
        ":stage_conversion",
        ":stage_to_block_conversion",
        ":stage_to_block_conversion_metadata",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:function_builder",
        "//xls/ir:ir_matcher",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "//xls/ir:value",
        "//xls/ir:verifier",
        "//xls/tools:codegen",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_test(
    name = "block_pipeline_inserter_pass_test",
    srcs = ["block_pipeline_inserter_pass_test.cc"],
    deps = [
        ":block_pipeline_inserter_pass",
        ":passes_ng_test_fixtures",
        ":stage_conversion",
        ":stage_to_block_conversion_pass",
        "//xls/codegen:codegen_options",
        "//xls/codegen:codegen_pass",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir:function_builder",
        "//xls/ir:verifier",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "flow_control_simplification",
    srcs = ["flow_control_simplification.cc"],
    hdrs = ["flow_control_simplification.h"],
    deps = [
        ":block_channel_slot",
        ":stage_to_block_conversion_metadata",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:op",
        "//xls/ir:source_location",
        "//xls/ir:value",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "flow_control_simplification_test",
    srcs = ["flow_control_simplification_test.cc"],
    deps = [
        ":block_channel_slot",
        ":flow_control_simplification",
        ":passes_ng_test_fixtures",
        ":stage_to_block_conversion_metadata",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir",
        "//xls/ir:bits",
        "//xls/ir:channel",
        "//xls/ir:function_builder",
        "//xls/ir:type",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "flow_control_simplification_pass",
    srcs = ["flow_control_simplification_pass.cc"],
    hdrs = ["flow_control_simplification_pass.h"],
    deps = [
        ":flow_control_simplification",
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_pass",
        "//xls/common/logging:log_lines",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status:statusor",
    ],
)

cc_test(
    name = "flow_control_simplification_pass_test",
    srcs = ["flow_control_simplification_pass_test.cc"],
    deps = [
        ":block_channel_slot",
        ":flow_control_simplification",
        ":passes_ng_test_fixtures",
        ":stage_to_block_conversion_metadata",
        "//xls/common:xls_gunit_main",
        "//xls/common/logging:log_lines",
        "//xls/common/status:matchers",
        "//xls/ir",
        "//xls/ir:channel",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/types:span",
        "@googletest//:gtest",
    ],
)

cc_library(
    name = "metadata_conversion_pass",
    srcs = ["metadata_conversion_pass.cc"],
    hdrs = ["metadata_conversion_pass.h"],
    deps = [
        ":stage_to_block_conversion_metadata",
        "//xls/codegen:codegen_pass",
        "//xls/common/logging:log_lines",
        "//xls/common/status:status_macros",
        "//xls/ir",
        "//xls/passes:pass_base",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status:statusor",
    ],
)
